package com.run.runbbs.controller;

import com.run.runbbs.Event.EventProducer;
import com.run.runbbs.common.lang.Result;
import com.run.runbbs.entity.Event;
import com.run.runbbs.entity.User;
import com.run.runbbs.service.impl.FollowService;
import com.run.runbbs.service.impl.UserService;
import com.run.runbbs.util.Constant;
import com.run.runbbs.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FollowController implements Constant {
    @Autowired
    private FollowService followService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private UserService userService;
    @Autowired
    private EventProducer eventProducer;


    @PostMapping("/follow")
    public Result follow(int entityType, int entityId, HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());

        followService.follow(userId,entityType,entityId);
        //触发关注事件（通知）:由于只有关注用户，没有其他可关注的实体，所以暂时setEntityUserId(entityId)
        Event event=new Event()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(userId)
                .setEntityType(entityType)
                .setEntityId(entityId)
                .setEntityUserId(entityId);
        if(event.getEntityUserId()!=userId){
            eventProducer.fireEvent(event);
        }



        return Result.success("关注成功！");
    }

    @PostMapping("/unFollow")
    public Result unFollow(int entityType, int entityId, HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());

        followService.unfollow(userId,entityType,entityId);

        return Result.success("取消关注成功！");
    }

    @GetMapping("/getFollowees")
    public Result getFollowees(int userId,int offset,HttpServletRequest request){

        int myUserId=0;
        String jwt=request.getHeader("Authorization");
        if(jwt!=null){
            myUserId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        }


        User user=userService.findUserById(userId);
        if(user==null){
            throw new RuntimeException("该用户不存在！");
        }

        List<User> userList=followService.findFollowees(userId,0,10);
        List<Map<String,Object>> followees=new ArrayList<>();

        if(userList!=null){
            for(User followee : userList){
                Map<String,Object> map=new HashMap<>();
                map.put("user",followee);
                //当前登录的用户是否关注
                boolean hasFollowed=false;
                if(myUserId!=0){
                    hasFollowed=followService.hasFollowed(myUserId,ENTITY_TYPE_USER,userId);
                }
                map.put("hasFollowed",hasFollowed);
                followees.add(map);
            }
        }
        return Result.success(followees);

    }

    @GetMapping("/getFollowers")
    public Result getFollowers(int userId,int offset,HttpServletRequest request){

        int myUserId=0;
        String jwt=request.getHeader("Authorization");
        if(jwt!=null){
            myUserId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        }


        User user=userService.findUserById(userId);
        if(user==null){
            throw new RuntimeException("该用户不存在！");
        }

        List<User> userList=followService.findFollowers(userId,0,10);
        List<Map<String,Object>> followers=new ArrayList<>();

        if(userList!=null){
            for(User followee : userList){
                Map<String,Object> map=new HashMap<>();
                map.put("user",followee);
                //当前登录的用户是否关注
                boolean hasFollowed=false;
                if(myUserId!=0){
                    hasFollowed=followService.hasFollowed(myUserId,ENTITY_TYPE_USER,userId);
                }
                map.put("hasFollowed",hasFollowed);
                followers.add(map);
            }
        }
        return Result.success(followers);

    }


}
